<?php

namespace app\admin\controller;

use app\admin\controller\Base;
use think\Request;
use think\Db;

class Images extends Base
{
    public function index($ty)
    {
        return $this->fetch('images');
    }

    public function getImg()
    {
        $ty = Request::instance()->param('ty');
        $res = preg_match('/ty\/(.*?)\//', $ty, $type);
        // return json($type);
        if ($res) {
            $img_arr = [];
            if ($type[1] == 'news') {
                $imgs = Db::name($type[1])->field('mid,img_url,title')->select();
                foreach ($imgs as $key => $val) {
                    $img_arr[$key]['src'] = $val['img_url'];
                    $img_arr[$key]['thumb'] = $val['img_url'];
                    $img_arr[$key]['alt'] = $val['title'];
                    $img_arr[$key]['pid'] = $val['mid'];
                }
            } elseif ($type[1] == 'case') {
                $imgs = Db::name($type[1])->field('cid,img_url,title')->select();
                foreach ($imgs as $key => $val) {
                    $img_arr[$key]['src'] = $val['img_url'];
                    $img_arr[$key]['thumb'] = $val['img_url'];
                    $img_arr[$key]['alt'] = $val['title'];
                    $img_arr[$key]['pid'] = $val['cid'];
                }
            }
            $data = [
                "title" => "图片管理",
                "id" => "Images",
                "start" => 0,
                "code" => 0,
                "data" => $img_arr,
            ];
            return json($data);
        }
    }
}
